package com.cogent.dynamicform.form.impl;

import java.util.HashMap;

import com.cogent.common.util.MapUtil;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.opensymphony.oscache.util.StringUtil;

public class RichTextDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		return "";
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap) + "&nbsp;";
		String csslink ="<link rel=\"stylesheet\" href=\"";
		String jslink ="<script charset=\"utf-8\" src=\"";
		StringBuffer str = new StringBuffer();
		String classname = "width : '600px',height : '350px',";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			classname = formColumnDomain.getShowclass();
		str.append(csslink).append("kindeditor/themes/default/default.css\"/>")
			.append("\n\t").append(csslink).append("kindeditor/plugins/code/prettify.css\"/>")
			.append("\n\t").append(jslink).append("kindeditor/kindeditor.js\"></script>")
			.append("\n\t").append(jslink).append("kindeditor/lang/zh_CN.js\"></script>")
			.append("\n\t").append(jslink).append("kindeditor/plugins/code/prettify.js\"></script>")
			.append("\n\t<script>").append("\n\t\t").append("KindEditor.ready(function(K) {")
			.append("\n\t\t\t").append("var editor1 = K.create('textarea[name=\"").append(formColumnDomain.getName()).append("\"]', {").append(classname)
			.append("\n\t\t").append("cssPath : '").append("kindeditor/plugins/code/prettify.css',")
			.append("\n\t\t").append("uploadJson : '").append("kindeditor/editor/upload_json.jsp',")
			.append("\n\t\t").append("fileManagerJson : '").append("kindeditor/editor/file_manager_json.jsp',")
			.append("\n\t\t").append("allowFileManager : true,")
			.append("\n\t\t").append("items:['source', '|', 'undo', 'redo', '|', 'print', 'cut', 'copy', 'paste',")
			.append("\n\t\t\t").append("'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright','justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',")
			.append("\n\t\t\t").append("'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/','formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',")
			.append("\n\t\t\t").append("'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image','flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'map', 'pagebreak','link', 'unlink'],")
			.append("\n\t\t\t").append("resizeType:0,").append("\n\t\t\t").append("afterCreate : function() {").append("\n\t\t\t\t")
			.append("var self = this;").append("\n\t\t\t\t").append("K.ctrl(document, 13, function() {").append("\n\t\t\t\t\t").append("self.sync();")
			.append("\n\t\t\t\t\t").append("document.forms['thisForm'].submit();").append("\n\t\t\t\t").append("});").append("\n\t\t\t\t").append("K.ctrl(self.edit.doc, 13, function() {")
			.append("\n\t\t\t\t\t").append("self.sync();").append("\n\t\t\t\t\t").append("document.forms['thisForm'].submit();").append("\n\t\t\t\t").append("});")
			.append("\n\t\t\t\t").append("}").append("\n\t\t").append("});").append("\n\t\t").append("prettyPrint();")
			.append("\n\t").append("});").append("</script>");
		str.append("<textarea name=\"").append(formColumnDomain.getName()).append("\" id=\"")
			.append(formColumnDomain.getName()).append("\">")
			.append(MapUtil.getStringValue(formMap, formColumnDomain.getName())).append("</textarea>");
		
		return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String name = formColumnDomain.getName();
		if(name.indexOf(".") > 0)
			name = name.substring(name.indexOf(".") + 1);
		return MapUtil.getStringValue(formMap, name, "&nbsp;");
	}
}
